﻿using Nasty.Core.LoadParams;
using Nasty.Core.Repository;
using Nasty.Core.Session;
using Nasty.Core.SuperExtension;
using Nasty.SystemModule.Areas.UserMessage.Model;
using Nasty.SystemModule.Areas.UserMessage.Params;
using Nasty.SystemModule.User;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Nasty.SystemModule.UserMessage
{
    public interface IUserMessageRepository : IRepository<Nasty.Core.Entity.System.UserMessage>
    {
        public Nasty.Core.Entity.System.UserMessage GetUserMessage(string id);

        public ResultData<Nasty.Core.Entity.System.UserMessage> SaveUserMessage(UserMessageModel model);

        public ResultData<string> DeleteUserMessages(List<string> ids);

        public PageData<Nasty.Core.Entity.System.UserMessage> GetUserMessagePage(GetUserMessagePageParams @params);

        public List<Nasty.Core.Entity.System.UserMessage> GetUserMessages(GetUserMessagesParams @params);

        public ResultData<int> GetUserMessageCount(GetUserMessageCountParams @params);

        public ResultData<string> SetAllUserMessageRead(SetAllUserMessageReadParams @params);
    }

    public class UserMessageRepository : SqlRepository<Nasty.Core.Entity.System.UserMessage>, IUserMessageRepository
    {
        public UserMessageRepository(SqlSugarClient db) : base(db)
        {
            //db.CodeFirst.InitTables(typeof(Nasty.Core.Entity.System.UserMessage));
        }

        public ResultData<string> DeleteUserMessages(List<string> ids)
        {
            var result = new ResultData<string>();
            try
            {
                Db.Delete<Nasty.Core.Entity.System.UserMessage>(ids);

                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        public Nasty.Core.Entity.System.UserMessage GetUserMessage(string id)
        {
            return this.Db.Queryable<Nasty.Core.Entity.System.UserMessage>().IncludesAllFirstLayer().InSingle(id);
        }

        public ResultData<Nasty.Core.Entity.System.UserMessage> SaveUserMessage(UserMessageModel model)
        {
            var result = new ResultData<Nasty.Core.Entity.System.UserMessage>();
            try
            {
                var data = Db.Save<Nasty.Core.Entity.System.UserMessage>(model);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        public PageData<Nasty.Core.Entity.System.UserMessage> GetUserMessagePage(GetUserMessagePageParams @params)
        {
            int totalPage = 0;
            int total = 0;
            var pageData = new PageData<Nasty.Core.Entity.System.UserMessage>();

            var _SQLExpress = Db.Queryable<Nasty.Core.Entity.System.UserMessage>().IncludesAllFirstLayer();

            if (!string.IsNullOrEmpty(@params.Title)) _SQLExpress.Where((t) => t.Title.Contains(@params.Title));
            if (!string.IsNullOrEmpty(@params.UserId)) _SQLExpress.Where((t) => t.UserId.Contains(@params.UserId));
            if (@params.MessageTypes != null) _SQLExpress.Where((t) => @params.MessageTypes.Contains(t.MessageType));
            _SQLExpress = _SQLExpress.OrderBy((t) => t.CreateTime, OrderByType.Desc);

            var data = _SQLExpress.ToPageList(@params.Current, @params.PageSize, ref total, ref totalPage);

            pageData.Total = total;
            pageData.TotalPage = totalPage;
            pageData.Data = data;

            pageData.Current = @params.Current;
            pageData.PageSize = @params.PageSize;
            return pageData;
        }

        public List<Nasty.Core.Entity.System.UserMessage> GetUserMessages(GetUserMessagesParams @params)
        {
            var _SQLExpress = Db.Queryable<Nasty.Core.Entity.System.UserMessage>();
            if (!string.IsNullOrEmpty(@params.Title)) _SQLExpress.Where((t) => t.Title.Contains(@params.Title));
            if (!string.IsNullOrEmpty(@params.UserId)) _SQLExpress.Where((t) => t.UserId.Contains(@params.UserId));
            _SQLExpress = _SQLExpress.OrderBy((t) => t.CreateTime, OrderByType.Desc);

            return _SQLExpress.ToList();
        }

        public ResultData<int> GetUserMessageCount(GetUserMessageCountParams @params)
        {
            var userId = AppSession.CurrentUser?.Value?.Id;

            var _SQLExpress = Db.Queryable<Nasty.Core.Entity.System.UserMessage>().Where((t) => t.UserId == userId);

            if (!string.IsNullOrEmpty(@params.Title)) _SQLExpress.Where((t) => t.Title.Contains(@params.Title));
            if (@params.IsRead != null) _SQLExpress.Where((t) => t.IsRead == @params.IsRead);
            if (@params.MessageType != null) _SQLExpress.Where((t) => t.MessageType == @params.MessageType);

            var count = _SQLExpress.Count();

            return new ResultData<int>()
            {
                IsSuccess = true,
                Data = count,
            };
        }

        public ResultData<string> SetAllUserMessageRead(SetAllUserMessageReadParams @params)
        {
            if (@params.MessageType != null)
            {
                Db.Updateable<Nasty.Core.Entity.System.UserMessage>()
                   .SetColumns(it => new Nasty.Core.Entity.System.UserMessage() { IsRead = true })
                   .Where((it) => !it.IsRead && it.MessageType == @params.MessageType)
                   .ExecuteCommand();
            }
            else
            {
                Db.Updateable<Nasty.Core.Entity.System.UserMessage>()
                   .SetColumns(it => new Nasty.Core.Entity.System.UserMessage() { IsRead = true })
                   .Where((it) => !it.IsRead)
                   .ExecuteCommand();
            }

            return ResultHelper<string>.Success(string.Empty);
        }
    }
}
